package edu.gatech.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import edu.gatech.presenters.AppointmentPresenter;
import edu.gatech.userclasses.Patient;
import edu.gatech.userclasses.User;
import edu.gatech.utilities.Persistence;

public class AppointmentPanel {
	private static JFrame appointment;
	static String[] months = { "January", "February", "March", "April",
		"May", "June", "July", "August", "September", "October", 
		"November", "December"};
	private static JTextField textField;
	private static JTextField textField_1;
	private static JTextField textField_2;
	private static JTextField textField_3;
	private static Patient currentPatient;
	private static JComboBox doctor;
	private static JRadioButton rdbtnEmergency;
	private static JTextArea textArea;
	private static JComboBox comboBox;
	private static ButtonGroup group;
	private static JRadioButton rdbtnAm;
	private static JRadioButton rdbtnPm;
	public AppointmentPanel(Patient curr){
		currentPatient = curr;
		appointment = new JFrame ("Create a New Appointment");
		JPanel appPanel = new JPanel();
		
		appointment.getContentPane().add(appPanel);
		appPanel.setLayout(null);
		
		JLabel lblDesiredTime = new JLabel("Desired Time:");
		lblDesiredTime.setBounds(69, 157, 73, 14);
		lblDesiredTime.setFont(new Font("Arial", Font.PLAIN, 10));
		appPanel.add(lblDesiredTime);
		
		JLabel lblDesiredMonth = new JLabel("Desired Month:");
		lblDesiredMonth.setBounds(69, 246, 73, 14);
		lblDesiredMonth.setFont(new Font("Arial", Font.PLAIN, 10));
		appPanel.add(lblDesiredMonth);
		
		JLabel lblDesiredDay = new JLabel("Desired Date:");
		lblDesiredDay.setBounds(68, 202, 73, 14);
		lblDesiredDay.setFont(new Font("Arial", Font.PLAIN, 10));
		appPanel.add(lblDesiredDay);
		
		comboBox = new JComboBox(months);
		comboBox.setBounds(147, 243, 132, 21);
		comboBox.setFont(new Font("Arial", Font.PLAIN, 10));
		appPanel.add(comboBox);
		
		textField = new JTextField();
		textField.setBounds(146, 199, 133, 20);
		textField.setFont(new Font("Arial", Font.PLAIN, 10));
		appPanel.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setBounds(147, 154, 85, 20);
		textField_1.setFont(new Font("Arial", Font.PLAIN, 10));
		appPanel.add(textField_1);
		textField_1.setColumns(10);
		
		group = new ButtonGroup();
		rdbtnAm = new JRadioButton("AM", true);
		rdbtnAm.setBounds(238, 139, 41, 23);
		rdbtnAm.setFont(new Font("Arial", Font.PLAIN, 10));
		group.add(rdbtnAm);
		appPanel.add(rdbtnAm);
		
		rdbtnPm = new JRadioButton("PM");
		rdbtnPm.setBounds(238, 162, 41, 23);
		rdbtnPm.setFont(new Font("Arial", Font.PLAIN, 10));
		group.add(rdbtnPm);
		appPanel.add(rdbtnPm);
		
		JLabel lblDesiredDoctor = new JLabel("Desired Doctor:");
		lblDesiredDoctor.setBounds(301, 114, 92, 14);
		lblDesiredDoctor.setFont(new Font("Arial", Font.PLAIN, 10));
		appPanel.add(lblDesiredDoctor);
		
		ArrayList<String> list = new ArrayList<String>();
		for (User u : Persistence.getUserList()){
			if (u.getUserType().equals("Doctor")){
				list.add(u.getName());
			}
		}
		String[] array = new String[list.size()];
		for(int i = 0; i < list.size(); i++){
			array[i] = list.get(i);
		}
		doctor = new JComboBox(array);
		doctor.setFont(new Font("Arial", Font.PLAIN, 10));
		doctor.setBounds(381, 112, 132, 20);
		appPanel.add(doctor);
		
		JLabel lblSymptoms = new JLabel("Symptoms:");
		lblSymptoms.setFont(new Font("Arial", Font.PLAIN, 10));
		lblSymptoms.setBounds(301, 159, 74, 14);
		appPanel.add(lblSymptoms);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("Monospaced", Font.PLAIN, 10));
		textArea.setBounds(301, 184, 212, 84);
		appPanel.add(textArea);
		
		rdbtnEmergency = new JRadioButton("URGENT\r\n");
		rdbtnEmergency.setFont(new Font("Arial", Font.PLAIN, 10));
		rdbtnEmergency.setBounds(440, 155, 73, 23);
		appPanel.add(rdbtnEmergency);
		
		JButton btnCreateAppointment = new JButton("Create Appointment");
		btnCreateAppointment.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				AppointmentPresenter presenter = new AppointmentPresenter(currentPatient);
				String amPm = "";
				if(rdbtnAm.isSelected()){
					amPm = "A.M";
				}
				else{
					amPm = "P.M";
				}
				String time = textField_1.getText() + " " + amPm;
				presenter.createAppointment(textArea.getText(), time, textField.getText(), (String)comboBox.getSelectedItem(), 
				(String)doctor.getSelectedItem(), rdbtnEmergency.isSelected());
				appointment.hide();
			}
		});
		btnCreateAppointment.setFont(new Font("Arial", Font.PLAIN, 10));
		btnCreateAppointment.setBounds(298, 298, 132, 23);
		appPanel.add(btnCreateAppointment);
		
		JLabel lblPatientName = new JLabel("Patient Name:");
		lblPatientName.setFont(new Font("Arial", Font.PLAIN, 10));
		lblPatientName.setBounds(69, 116, 73, 14);
		appPanel.add(lblPatientName);
		
		textField_3 = new JTextField();
		textField_3.setText(currentPatient.getName());
		textField_3.setEditable(false);
		textField_3.setBounds(147, 112, 132, 20);
		appPanel.add(textField_3);
		textField_3.setColumns(10);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				appointment.hide();
			}
		});
		btnCancel.setFont(new Font("Arial", Font.PLAIN, 10));
		btnCancel.setBounds(440, 298, 73, 23);
		appPanel.add(btnCancel);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Create Appointment", TitledBorder.LEADING, TitledBorder.TOP, null, UIManager.getColor("CheckBox.darkShadow")));
		panel.setBounds(47, 83, 484, 204);
		appPanel.add(panel);
		
		MenuBar menuBar = new MenuBar(appointment);
        
        appointment.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        appointment.setSize(600,480); 
        appointment.setVisible(true);
	}
}
